package com.hsource.doctorcenter.webSocket;

import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Header;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/30 11:35
 * @describe
 */
@Slf4j
public class WebSocketUtil {

    /**
     *
     * @param imgUrl 图片地址
     * @return
     */
    public static BufferedImage getBufferedImage(String imgUrl) {
        URL url = null;
        InputStream is = null;
        BufferedImage img = null;
        try {
            url = new URL(imgUrl);
            is = url.openStream();
            img = ImageIO.read(is);
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } finally {
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
            }
        }
        return img;
    }


    /**
     * 读取语音的时长
     * @param fileUrl
     * @return
     * @throws MalformedURLException
     */
    public static int getDuration(String fileUrl) throws MalformedURLException {
        URL url = new URL(fileUrl);
        URLConnection con = null;
        try {
            con = url.openConnection();
            BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
            Bitstream bt = new Bitstream(bis);
            //获取mp3时间长度
            Header header = bt.readFrame();
            int mp3Length = con.getContentLength();
            int time = (int) header.total_ms(mp3Length) / 1000;
            return time;
        } catch (BitstreamException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return 0;
    }

//    public static void main(String[] args) {
////        BufferedImage image = getBufferedImage("https://hsource-oss.oss-cn-beijing.aliyuncs.com/20201030/2020-10-30-09%3A25%3A37-0.png");
////        System.out.println(image.getWidth()+"-------"+image.getHeight());
////        String asdasd="123";
////        System.out.println(asdasd.split(",").length);
//        int a=(int)(Math.random()*(3))+1;
//        System.out.print(a);
//    }
}
